package com.jspgou.cms.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.jspgou.cms.dao.StoreModelDao;
import com.jspgou.cms.entity.StoreModel;
import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;

@Repository
public class StoreModelDaoImpl extends HibernateBaseDao<StoreModel, Long> implements StoreModelDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}

	public StoreModel findById(Long id) {
		StoreModel entity = get(id);
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	public List<StoreModel> getList(){
		Finder f=Finder.create("select bean from StoreModel bean ");
		return find(f);
	}
	
	public StoreModel getByDefaultModel(Boolean defaultModel){
		Finder f = Finder.create("from StoreModel bean where bean.defaultModel=:defaultModel");
		f.setParam("defaultModel", defaultModel);
		Query query = f.createQuery(getSession());
		query.setMaxResults(1);
		return (StoreModel)query.uniqueResult();
	}

	public StoreModel save(StoreModel bean) {
		getSession().save(bean);
		return bean;
	}

	public StoreModel deleteById(Long id) {
		StoreModel entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<StoreModel> getEntityClass() {
		return StoreModel.class;
	}
}